<?php

namespace App\Services\Common;

use App\Models\Account\UserAccountModel;
use App\Models\Account\UserAccountSkuModel;
use App\Models\Register\ActivateUserManagementModel;
use Illuminate\Support\Facades\Log;

/**
 * 公共函数类
 */
class CommonService
{
    //常见的邮箱地址格式
    public $emailList = [
        '@qq.com',
        'bellsouth.net',
        '@gmail.com',
        '@hotmail.com',
        '@yahoo.com',
        '@aol.com',
        '@btinternet.com',
        '@outlook.com',
        '@virginmedia.com',
        '@sky.com',
        '@talktalk.net',
        '@protonmail.com',
        '@fastmail.com',
        '@gmx.de',
        '@web.de',
        '@freenet.de',
        '@yahoo.de',
        '@hotmail.de',
        '@outlook.de',
        '@t-online.de',
        '@1und1.de',
        '@zoho.com',
        '@libero.it',
        '@tin.it',
        '@hotmail.it',
        '@yahoo.it',
        '@virgilio.it',
        '@alice.it',
        '@fastwebnet.it',
        '@libero.com',
        '@tim.it',
        '@telefonica.net',
        '@orange.es',
        '@wanadoo.es',
        '@yahoo.es',
        '@outlook.es',
        '@terra.es',
        '@ono.com',
        '@yahoo.co.jp',
        '@hotmail.co.jp',
        '@docomo.ne.jp',
        '@ezweb.ne.jp',
        '@softbank.ne.jp',
        '@y-mobile.ne.jp',
        '@outlook.jp',
        '@nifty.com',
        '@au.com',
        '@icloud.com',
        '@wakuwaku.ne.jp'

    ];

    /**
     * 验证邮箱格式
     * @return boolean
     */
    public function emailPreg($email)
    {
        $preg_email = '/^[_a-zA-Z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/i';
        if (preg_match($preg_email, $email)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @Notes:验证各个国家常见的邮箱格式
     * @Interface emailFormat
     * @param $email
     * @author: wuyu
     * @Time: 2023/7/3   19:03
     */
    public function emailFormat($email)
    {
        Log::channel('registerLog')->debug('用户注册邮箱填写格式:',['email' => $email]);
        return true;
        $emailList = $this->emailList;
        $isEmail = false;
        foreach ($emailList as $emailV){
            if (strpos($email, $emailV) !== false){
                $isEmail = true;
                break;
            }
        }
        return $isEmail;
    }

    /**
     * @Notes:用户获取对应权限
     * @Interface userAuth
     * @author: wuyu
     * @Time: 2023/7/13   16:52
     */
    public static function userAuth()
    {
        $createUser = [];
        $UserAccountModel = new UserAccountModel();
        if (!in_array($UserAccountModel->userId,$UserAccountModel->userAllIds)){
            //如果是组长
            $userGroupIdOne = $UserAccountModel->getOne([
                ['user_id','=',$UserAccountModel->userId]
            ]);
            if (!empty($userGroupIdOne)){
                $userGroupList = $UserAccountModel->where('user_id','=',$userGroupIdOne['user_id'])->where('mark','=',1)->get(['user_id','user_group_id'])->toArray();
                if (!empty($userGroupList)){
                    $createUser = array_unique(array_column($userGroupList,'user_group_id'));
                }
            }else{
                $createUser = [$UserAccountModel->userId];
            }
        }

        return $createUser;
    }

    /**
     * @Notes:用户获取sku对应的权限
     * @Interface skuAuth
     * @author: wuyu
     * @Time: 2023/7/13   18:07
     */
    public static function skuAuth()
    {

        $skuArr = [];
        $UserAccountModel = new UserAccountModel();
        $UserAccountSkuModel = new UserAccountSkuModel();
        $userId = $UserAccountModel->userId;
        if (!in_array($userId,$UserAccountModel->userAllIds)){
            //如果是组长
            $userGroupIdOne = $UserAccountModel->getOne([
                ['user_id','=',$userId]
            ]);
            if (!empty($userGroupIdOne)){
                $userGroupList = $UserAccountModel->where('user_id','=',$userGroupIdOne['user_id'])->where('mark','=',1)->get(['id'])->toArray();
                if (!empty($userGroupList)){
                    $idsArr = array_unique(array_column($userGroupList,'id'));
                    $skuList = $UserAccountSkuModel->whereIn('user_account_id',$idsArr)->get()->toArray();
                    $skuArr = array_unique(array_column($skuList,'sku'));
                }
            }else{
                $userAccountOne = $UserAccountModel->getOne([
                    ['user_group_id','=',$userId]
                ]);
                if (!empty($userAccountOne)){
                    $skuList = $UserAccountSkuModel->where('user_account_id','=',$userAccountOne['id'])->get()->toArray();
                    $skuArr = array_unique(array_column($skuList,'sku'));
                }
            }
            if (empty($skuArr)) $skuArr = ['A1'];
        }

        return $skuArr;

    }

    /**
     * @Notes:获取asin对应权限
     * @Interface asinAuth
     * @author: wuyu
     * @Time: 2023/7/14   12:00
     */
    public static function asinAuth()
    {

        $asinArr = [];
        $UserAccountModel = new UserAccountModel();
        $ActivateUserManagementModel = new ActivateUserManagementModel();
        $userId = $UserAccountModel->userId;
        if (!in_array($userId,$UserAccountModel->userAllIds)){
            //如果是组长
            $userGroupIdOne = $UserAccountModel->getOne([
                ['user_id','=',$userId]
            ]);
            if (!empty($userGroupIdOne)){
                $userGroupList = $UserAccountModel->where('user_id','=',$userGroupIdOne['user_id'])->where('mark','=',1)->get(['user_group_id'])->toArray();
                if (!empty($userGroupList)){
                    $idsArr = array_unique(array_column($userGroupList,'user_group_id'));
                    $asinList = $ActivateUserManagementModel->whereIn('create_user',$idsArr)->get(['asin'])->toArray();
                    $asinArr = array_unique(array_column($asinList,'asin'));
                }
            }else{
                $asinList = $ActivateUserManagementModel->where('create_user','=',$userId)->get()->toArray();
                $asinArr = array_unique(array_column($asinList,'asin'));
            }
            if (empty($asinArr)) $asinArr =['A1'];
        }
        return $asinArr;
    }



}

